package core;

import gitobject.Blob;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import repository.Repository;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

@Command(
        name = "add"
)
/**
 * 实现了jit add命令，作用为将文件提交到暂存区
 * 形式为jit add -"文件名"
 */
public class JitAdd implements Runnable{
    @Option(names = {"-A","--all"})
    private boolean allFiles;
    @Parameters(paramLabel = "<files>")
    private List<Path> files;
    @Override
    public void run() {
        try {
            if(!allFiles && (files == null || files.isEmpty())){
                System.err.println("You need give the files or use -A | --all");
                System.exit(-1);
            }
            Repository repo = Repository.getInstance();
            var index = repo.getIndex();
            var stage = index.getStage();
            if(allFiles){
                Files.walkFileTree(repo.getWorkTree(), new SimpleFileVisitor<Path>(){
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        if(repo.getWorkTree().relativize(dir).toString().equals(".jit")){
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                        return super.preVisitDirectory(dir, attrs);
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Path path = repo.getWorkTree().relativize(file.toAbsolutePath());
                        if(!stage.containsKey(path)){
                            Blob blob = new Blob(path);
                            blob.write();
                            index.add(blob);
                            System.out.println("Add: " + path);
                        }
                        return super.visitFile(file, attrs);
                    }
                });
            } else {
                for (var path: files){
                    if(path.toFile().exists()){
                        Blob blob = new Blob(path);
                        blob.write();
                        index.add(blob);
                        System.out.println("Add: " + path);
                    } else {
                        if(index.getStage().containsKey(path)){
                            index.getStage().remove(path);
                        }else {
                            System.err.println("File Not found: " + path.toString());
                        }
                    }
                }
            }
            index.write();
        } catch (Exception err){
            throw new RuntimeException(err);
        }
    }
}
